<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div v-for="(item,index) in list" :key="item.roleId" class="role-item" :class="{active:index==roleList}" @click="convert(index)">
        <div class="role-info">
          <svg-icon :icon-class="index==roleList? 'user-active':'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push(`/addRole?id=${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delRoleUser(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/addRole')">添加角色</el-button>
    </div>

    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="permission">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> 楼宇管理 </div>
              <el-tree
                ref="tree"
                :data="item.children"
                :props="defaultProps"
                :default-expand-all="true"
                show-checkbox
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>

        <el-tab-pane :label="`成员(${userTotal})`" name="user">
          <div class="user-wrapper">
            <el-table
              :data="rowLsit"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>

    </div>
  </div>
</template>
<script>
import { getSystemRoleApi, getTreeListAPI, getRoleDetailAPI, getRoleUserAPi, delRoleUserAPI } from '@/api/system'
export default {
  name: 'Role',
  data() {
    return {
      list: [],
      roleList: 0,
      treeList: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      activeName: 'permission',
      params: {
        page: '1',
        pageSize: '10'
      },
      userTotal: '',
      rowLsit: []
    }
  },
  // 添加async await 是为了让这两个函数先执行完成后再让另一个函数执行
  async created() {
    await this.getSystemRole()
    await this.getTreeList()
    this.convert(0)
  },
  methods: {
    // 删除角色
    async delRoleUser(roleId) {
      const res = await delRoleUserAPI(roleId)
      this.$message.success('删除成功')
        .this.getSystemRole()
      console.log(res)
    },
    // 获取不同角色对应的权限
    async getRoleDetail(roleId) {
      const res = await getRoleDetailAPI(roleId)
      // console.log(res)
      const perms = res.data.perms
      // console.log(perms)
      const refsTree = this.$refs.tree
      // console.log(refsTree)
      refsTree.forEach((tree, index) => {
        tree.setCheckedKeys(perms[index])
      })
    },
    addDisabled(treeList) {
      // 使用forEach会改变遍历的数据的值
      // 使用map不会改变数据的值
      treeList.forEach(item => {
        item.disabled = true
        // console.log(item)
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
    },
    async getTreeList() {
      const res = await getTreeListAPI()
      this.treeList = res.data
      this.addDisabled(this.treeList)
    },
    // 实现点击切换
    async convert(index) {
      // console.log(index)
      this.roleList = index
      const roleId = this.list[index].roleId
      // console.log(roleId)
      this.getRoleDetail(roleId)
      // 成员人数获取
      const res = await getRoleUserAPi(roleId, this.params)
      // console.log(res)
      this.userTotal = res.data.total
      this.rowLsit = res.data.rows
    },
    // 数据获取
    async getSystemRole() {
      const res = await getSystemRoleApi()
      // console.log(res)
      this.list = res.data
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
